package com.basic.app.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 功能描述
 *
 * @author ganxinge
 * @date 2020/7/1820:21
 */
@JsonInclude(Include.NON_NULL)
@ApiModel(value = "响应结果",description="响应结果对象")
public class AppResponse<T> implements Serializable {

    /**
     * 响应状态码，请求成功或请求失败（非业务原因导致的失败），非空
     */
    @ApiModelProperty(value = "响应状态码",name="code",required=true,example="0000")
    private String code;
    /**
     * 响应提示文本，非空
     */
    @ApiModelProperty(value = "响应提示文本",name="msg",required=true,example="请求成功")
    private String msg;
    /**
     * 具体的业务参数，可空
     */
    @ApiModelProperty(value = "响应业务数据",name="data")
    private T data;

    /**
     * 返回成功的响应
     * @param message
     * @param <T>
     * @return
     */
    public static<T> AppResponse<T> success(String message){
        String code = AppResponseCode.CODE_SUCCESS.getCode();
        return new AppResponse(code,message);
    }

    /**
     * 返回失败的响应
     * @param message
     * @param <T>
     * @return
     */
    public static<T> AppResponse<T> error(String message){
        String code = AppResponseCode.CODE_ERROR_FAILED.getCode();
        return new AppResponse(code,message);
    }

    /**
     * 返回成功的数据结果
     * @param data
     */
    public AppResponse(T data){
        this.code = AppResponseCode.CODE_SUCCESS.getCode();
        this.msg = AppResponseCode.CODE_SUCCESS.getMsg();
        this.setData(data);
    }

    /**
     * 返回带有业务状态的数据结果
     * @param code
     * @param data
     */
    public AppResponse(AppResponseCode code,T data){
        this.code = code.getCode();
        this.msg = code.getMsg();
        this.setData(data);
    }


    private AppResponse(String code,String message){
        this.code = code;
        this.msg = message;
    }


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
